import { hostname } from '../config/host.config'
import axios from '../config/axios'
import Router from "next/router"
import { message } from 'antd'

const staticBlogText = '### 请使用MarkDown格式进行编写' + '\n' + '编写内容'
const staticBlogShortContent = '进行编写'
export const blogState = {
    content: staticBlogText,
    shortContent: staticBlogShortContent,
    kind: 'JavaScript',
    title: '',
    id: 0,
    doType: 'add',
    reviewFlag: false,
    articleList: []
}

export const actionTypes = {
    changeContent: 'changeContent',
    changeTitle: 'changeTitle',
    changeKind: 'changeKind',
    changeId: 'changeId',
    changeAll: 'changeAll',
    changeDoType: 'changeDoType',
    changeReviewFlag: 'changeReviewFlag',
    changeArticleList: 'changeArticleList',
    reset: 'reset'
}

export const BlogReducer = (state = blogState, action) => {
    switch (action.type) {
        case actionTypes.changeContent:
            return Object.assign({}, state, { content: action.content, shortContent: action.shortContent })
        case actionTypes.changeTitle:
            return Object.assign({}, state, { title: action.title })
        case actionTypes.changeKind:
            return Object.assign({}, state, { kind: action.kind })
        case actionTypes.changeReviewFlag:
            return Object.assign({}, state, { reviewFlag: action.reviewFlag })
        case actionTypes.changeAll:
            return Object.assign({}, state, {
                content: action.content,
                shortContent: action.shortContent,
                title: action.title,
                kind: action.kind,
                id: action.id
            })
        case actionTypes.changeId:
            return Object.assign({}, state, { id: action.id })
        case actionTypes.changeDoType:
            return Object.assign({}, state, { doType: action.doType })
        case actionTypes.changeArticleList:
            return Object.assign({}, state, { articleList: action.articleList })
        case actionTypes.reset:
            return Object.assign({}, state, blogState, { articleList: state.articleList })
        default:
            return state
    }
}

// 公用方法
async function updateArticle() {
    const res = await axios.get(hostname + '/blog/user/article')
    if (res.status === 200 && res.data.statusCode === 1) {
        const data = res.data.statusMsg
        let articleList = []
        for (const item of data) {
            const menuLeftList = {
                title: '',
                id: 0
            }
            menuLeftList.title = item.title
            menuLeftList.id = item.id
            articleList.push(menuLeftList)
        }
        return articleList
    } else {
        return []
    }
}

export const changeContent = ({ content, shortContent }) => dispatch => {
    dispatch({ type: 'changeContent', content, shortContent })
}

export const changeTitle = (title) => dispatch => {
    dispatch({ type: 'changeTitle', title })
}

export const changeKind = (kind) => dispatch => {
    dispatch({ type: 'changeKind', kind })
}

export const changeReviewFlag = (reviewFlag) => async dispatch => {
    dispatch({ type: 'changeReviewFlag', reviewFlag })
}

export const changeArticleList = () => async dispatch => {
    const articleList = await updateArticle()
    dispatch({ type: 'changeArticleList', articleList })
}


export const changeId = (id = 0) => async dispatch => {
    if (id !== 0) {
        const res = await axios.post(hostname + '/blog/article', {
            articleId: id
        })
        if (res.status === 200 && res.data.statusCode === 1) {
            const { content, shortContent, title, kind, id } = res.data.statusMsg.list
            dispatch({ type: 'changeAll', content, shortContent, title, kind, id })
        }
    } else {
        dispatch({ type: 'changeId', id })
    }
}

export const changeAll = ({ content, shortContent, title, kind, id }) => dispatch => {
    dispatch({ type: 'changeAll', content, shortContent, title, kind, id })
}

export const changeDoType = (doType) => dispatch => {
    dispatch({ type: 'changeDoType', doType })
}

export const reset = () => async dispatch => {
    dispatch({ type: 'reset' })
}
export const newBlog = ({ content, shortContent, kind, title }) => async dispatch => {
    let res = await axios.post(hostname + '/blog/new', { content, shortContent, kind, title })
    if (res.status === 200 && res.data.statusCode === 1) {
        message.success('发送成功 请在过去文章中继续编辑!')
        dispatch({ type: 'reset' })
        const articleList = await updateArticle()
        dispatch({ type: 'changeArticleList', articleList })
    } else {
        message.error('发生错误')
    }
}

export const updateBlog = ({ content, shortContent, kind, title, id }) => async dispatch => {
    let res = await axios.post(hostname + '/blog/article/update', { content, shortContent, kind, title, id })
    if (res.status === 200 && res.data.statusCode === 1) {
        message.success('更新成功')
        const articleList = await updateArticle()
        dispatch({ type: 'changeArticleList', articleList })
    } else {
        message.error('发生错误')
    }
}

export const deleteBlog = (id) => async dispatch => {
    let res = await axios.post(hostname + '/blog/delete', { id })
    if (res.status === 200 && res.data.statusCode === 1) {
        message.success('删除成功')
        const articleList = await updateArticle()
        dispatch({ type: 'reset' })
        dispatch({ type: 'changeArticleList', articleList })
        setTimeout(() => {
            Router.push('/blog')
        }, 200)
    } else {
        message.error('发生错误')
    }
}
